package com.dacool.fragment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout.LayoutParams;

import com.dacool.activity.BaseActivity;
import com.dacool.app.sale.R;
import com.dacool.dialog.MyProgressDialog;
import com.dacool.util.ViewUtils;
import com.dacool.view.TitleItem;

public abstract class BaseFragment extends Fragment implements OnClickListener {
	protected TitleItem mTitleItem;
	protected TitleItem mRightItem;
	protected TitleItem mLeftItem;
	protected TitleItem mSecondLeftItem;

	public MyProgressDialog progressDialog;

	private boolean status;

	private ContainerFragment mContainer;

	public void setContainerFragment(ContainerFragment frag) {
		this.mContainer = frag;
	}

	public ContainerFragment getContainerFragment() {
		return mContainer;
	}

	public TitleItem getRightItem() {
		if (getActivity() == null) {
			return null;
		}
		return mRightItem;
	}

	public TitleItem getSecondLeftItem() {
		if (getActivity() == null) {
			return null;
		}
		if (showDefaultSecondLeftButton())
			if (mSecondLeftItem == null) {
				ImageView iv = new ImageView(getActivity());
				iv.setImageResource(R.drawable.icon_back);
				iv.setScaleType(ScaleType.FIT_CENTER);
				iv.setBackgroundColor(Color.TRANSPARENT);
				iv.setOnClickListener(this);
				mSecondLeftItem = new TitleItem();
				mSecondLeftItem.width = ViewUtils.rp(60);
				mSecondLeftItem.height = ViewUtils.rp(30);
				mSecondLeftItem.view = iv;
			}
		return mSecondLeftItem;
	}

	protected boolean showDefaultSecondLeftButton() {
		return false;
	}

	protected boolean showDefaultLeftButton() {
		return false;
	}

	public TitleItem getLeftItem() {
		if (getActivity() == null) {
			return null;
		}
		if (showDefaultLeftButton())
			if (mLeftItem == null) {
				ImageView iv = new ImageView(getActivity());
				iv.setImageResource(R.drawable.img_title);
				iv.setScaleType(ScaleType.FIT_CENTER);
				iv.setBackgroundColor(Color.TRANSPARENT);
				iv.setOnClickListener(this);
				mLeftItem = new TitleItem();
				mLeftItem.width = ViewUtils.rp(60);
				mLeftItem.height = ViewUtils.rp(30);
				mLeftItem.view = iv;
			}
		return mLeftItem;
	}

	public TitleItem getTitleItem() {
		if (getActivity() == null) {
			return null;
		}
		if (mTitleItem == null) {
			if (this.titleText() == null) {
				ImageView iv = new ImageView(getActivity());
				iv.setImageResource(R.drawable.img_title1);
				iv.setScaleType(ScaleType.FIT_CENTER);
				iv.setBackgroundColor(Color.TRANSPARENT);
				mTitleItem = new TitleItem();
				mTitleItem.view = iv;
				mTitleItem.width = ViewUtils.rp(150);
				mTitleItem.height = LayoutParams.WRAP_CONTENT;
			} else {
				TextView textView = new TextView(getActivity());
				textView.setTextColor(Color.WHITE);
				textView.setText(this.titleText());
				textView.setBackgroundColor(Color.TRANSPARENT);
				textView.setTextSize(22);
				textView.setTypeface(null, Typeface.BOLD);
				mTitleItem = new TitleItem();
				mTitleItem.view = textView;
			}

		}

		return mTitleItem;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = myView();
		if (view == null)
			view = inflater.inflate(myLayoutID(), null);
		view.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				v.onTouchEvent(event);
				return true;
			}
		});
		return view;
	}

	public final BaseActivity getContainerActivity() {
		return (BaseActivity) super.getActivity();
	}

	protected abstract int myLayoutID();

	protected abstract View myView();

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
	}

	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
	}

	@Override
	public View getView() {
		return super.getView();
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public boolean getStatus() {
		return this.status;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getContainerActivity().onFragmentAttached(this);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (mLeftItem != null && v == mLeftItem.view) {
			if (getContainerActivity().getMainActivity() != null) {
				getContainerActivity().popToRoot();
				getContainerActivity().getMainActivity().setCurrentTab(0);
			}
		} else if (mSecondLeftItem != null && v == mSecondLeftItem.view) {
			getContainerActivity().handleBackButton();
		}
	}

	public String titleText() {
		return null;
	}

	public void showProgressDialog(String msg, final boolean cancelable) {
		ViewUtils.runInMainThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (progressDialog == null) {
					progressDialog = new MyProgressDialog(getActivity());
				}
				progressDialog.setCancelable(cancelable);
				progressDialog.show();
			}
		});

	}

	public void dismissProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	public boolean handleBackButton() {
		return false;
	}

}
